var easyTemplate = function(s,d){
    if(!s){return '';}  
    if(s!==easyTemplate.template){  
        easyTemplate.template = s;  
        easyTemplate.aStatement = easyTemplate.parsing(easyTemplate.separate(s));  
   }  
    var aST = easyTemplate.aStatement;
    var process = function(d2){ 
        if(d2){d = d2;}  
        return arguments.callee;  
    };  
    process.toString = function(){  
        return (new Function(aST[0],aST[1]))(d);  
    };  
    return process;  
};  
easyTemplate.separate = function(s){
    var r = /\\'/g; 
    var sRet = s.replace(/(<(\/?)#(.*?(?:\(.*?\))*)>)|(')|([\r\n\t])|(\$\{([^\}]*?)\})/g,function(a,b,c,d,e,f,g,h){
        if(b){return '{|}'+(c?'-':'+')+d+'{|}';}  
        if(e){return '\\\'';}  
        if(f){return '';}  
        if(g){return '\'+('+h.replace(r,'\'')+')+\'';}  
    });  
    return sRet;  
};  
easyTemplate.parsing = function(s){
    var mName,vName,sTmp,aTmp,sFL,sEl,aList,aStm = ['var aRet = [];'];  
    aList = s.split(/\{\|\}/);  
    var r = /\s/;  
    while(aList.length){ 
        sTmp = aList.shift();  
        if(!sTmp){continue;}  
        sFL = sTmp.charAt(0);  
        if(sFL!=='+'&&sFL!=='-'){  
            sTmp = '\''+sTmp+'\'';aStm.push('aRet.push('+sTmp+');');  
            continue;  
        }  
        aTmp = sTmp.split(r);  
        switch(aTmp[0]){  
            case '+macro':mName = aTmp[1];vName = aTmp[2];aStm.push('aRet.push("<!--'+mName+' start--\>");');break;  
            case '-macro':aStm.push('aRet.push("<!--'+mName+' end--\>");');break;
            case '+assign':
            	aTmp.splice(0,1);
            	if(aTmp[0].contains("?int")){
	        		temp = aTmp[0].substr(aTmp[0].indexOf("(data."),aTmp[0].indexOf("?int")+4);
	        		afTemp = "parseInt("+aTmp[0].substr(aTmp[0].indexOf("data."),aTmp[0].indexOf(")?int")-aTmp[0].indexOf("data."))+")";
	        		aTmp[0] = aTmp[0].replace(temp,afTemp);
	        	}
            	aStm.push("var  " + aTmp.join(' ')+';');break;
            case '+if':aTmp.splice(0,1);aStm.push('if'+aTmp.join(' ')+'{');break;  
            case '+elseif':aTmp.splice(0,1);aStm.push('}else if'+aTmp.join(' ')+'{');break;  
            case '-if':aStm.push('}');break;  
            case '+else':aStm.push('}else{');break; 
            case '+list':
            	if(aTmp[1].contains("..")){
            		var start = aTmp[1].substr(0,aTmp[1].indexOf(".."));
            		var end = aTmp[1].substr(aTmp[1].indexOf("..")+2,aTmp[1].length);
            		aStm.push('if('+end+' >= '+ start
            				+'){ var c,_list=[]; for( var c='+ start +'; c<='+ end +'; c++){ _list.push(c); } with({i:0,l:_list.length,'+aTmp[3]+'_index:0,'+aTmp[3]+':null,'+aTmp[3]+'_has_next:true}){for(i=l;i--;){'+aTmp[3]+'_index=(l-i-1);'+aTmp[3]+'=_list['+aTmp[3]+'_index];'+aTmp[3]+'_has_next=!(i!==l-1);');
            	}else{
            		aStm.push('if('+aTmp[1]+'.constructor === Array){with({i:0,l:'+aTmp[1]+'.length,'+aTmp[3]+'_index:0,'+aTmp[3]+':null,'+aTmp[3]+'_has_next:true}){for(i=l;i--;){'+aTmp[3]+'_index=(l-i-1);'+aTmp[3]+'='+aTmp[1]+'['+aTmp[3]+'_index];'+aTmp[3]+'_has_next=!(i!==l-1);');
            	}
            	break;
            case '-list':aStm.push('}}}');break;  
            default:break;  
        }  
    }  
    aStm.push('return aRet.join("");');  
    if(!vName){aStm.unshift('var data = arguments[0];');}  
    return [vName,aStm.join('')];  
};
easyTemplate.sequence = function(d,b){
	var c,a=[];
	for(var c=d; c<=b; c++){
		a.push(c);
	}
	return a;
};
